package com.bwei.like.mynewstop.utils;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.bwei.like.mynewstop.app.MyApplication;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * Created by LiKe on 2016/10/31.
 */
public class CommonUtils {

    public static final String TAG = "NEWSTOP";
    private static SharedPreferences sharedPreferences;

    //打气
    public static View inflter(int loyoutId) {
        View view = View.inflate(MyApplication.getContext(), loyoutId, null);
        return view;
    }

    /**
     * dip  相对像素转换为  px绝对像素
     *
     * @param dip
     * @return
     */
    public static int dip2px(int dip) {
        //获取像素密度
        float density = MyApplication.getContext().getResources().getDisplayMetrics().density;
        //转换
        int px = (int) (dip * density + 0.5f);
        return px;
    }

    /**
     * px 相对像素转换为  dip 绝对像素
     *
     * @param px
     * @return
     */
    public static int px2dip(int px) {
        //获取像素密度
        float density = MyApplication.getContext().getResources().getDisplayMetrics().density;
        //转换
        int dip = (int) (px / density + 0.5f);
        return dip;
    }

    //获取资源文件中字符串
    public static String getString(int stringId) {
        String string = MyApplication.getContext().getResources().getString(stringId);
        return string;
    }

    //获取资源文件的drawable文件
    public static Drawable getDrawable(int did) {
        return MyApplication.getContext().getResources().getDrawable(did);
    }

    //创建sheardprefrences
    public static void saveSp(String flag, String str) {
        if (sharedPreferences == null) {
            sharedPreferences = MyApplication.getContext().getSharedPreferences(TAG, MyApplication.getContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(flag, str);
        edit.commit();
    }

    //清空之前保存的titles数据
    public static void removeSp(String flag){
        if (sharedPreferences == null) {
            sharedPreferences = MyApplication.getContext().getSharedPreferences(TAG, MyApplication.getContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.remove(flag);
        edit.commit();
    }

    //存储boolean值
    public static void saveBoolean(String flag,boolean boo) {
        if (sharedPreferences == null) {
            sharedPreferences = MyApplication.getContext().getSharedPreferences(TAG, MyApplication.getContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(flag,boo);
        edit.commit();
    }


    //获取boolean值
    public static boolean getBooleanFlag(String flag) {
        if (sharedPreferences == null) {
            sharedPreferences = MyApplication.getContext().getSharedPreferences(TAG, MyApplication.getContext().MODE_PRIVATE);
        }
        boolean sharedPreferencesBoolean = sharedPreferences.getBoolean(flag, false);
        return sharedPreferencesBoolean;
    }

    //获取值
    public static String getSp(String flag) {
        if (sharedPreferences == null) {
            sharedPreferences = MyApplication.getContext().getSharedPreferences(TAG, MyApplication.getContext().MODE_PRIVATE);
        }
        String string = sharedPreferences.getString(flag, "");
        return string;
    }

    //判断当前任务是否在主线程中，如果在主线程中直接执行，否则通过handler执行
    public static void runOnUIThread(Runnable runnable) {
        if (android.os.Process.myTid() == MyApplication.getMainThreadId()) {
            runnable.run();
        } else {
            MyApplication.getHandler().post(runnable);
        }
    }

    //使用线程池执行runnable
    public static void executeRunnable(Runnable runnable) {
        MyApplication.getThreadPoll().execute(runnable);
    }

    public static DisplayImageOptions getInitOptions(){
       return  ImageLoaderUtils.initOptions2();
    }
}
